<?php
namespace app\manage\controller;
use think\Db;

class PayOrder extends Base
{
    public function index(){
        if(request()->isAjax() || input('?export')){
            $page = input('page');
            $limit = input('limit');
            $plat = input('plat');
            $date = explodeTime(input('date'));
            $myOrderId = input('my_order_id');
            $gameId = input('game_id');
            $serverId = input('server_id');
            $channelId = input('channel_id');
            $gameResult = input('game_result');
            $debug = input('debug');
            $payType = input('pay_type');

            $where = '1';
            $where .= model('manage/Game')->gameClient(input('game_server'), input('game_publish'), input('game_platform'), input('game_client'));
            $where .= " && pay_time between '{$date['start_time']}' AND '{$date['end_time']}'";
            if(!empty($plat)){
                if($plat == 'cps'){
                    $where .= " && channel_id='1000'";
                }else{
                    $where .= " && channel_id>='5000'";
                }
            }
            !empty($myOrderId) && $where .= " && my_order_id='{$myOrderId}'";
            !empty($gameId) && $where .= " && game_id='{$gameId}'";
            !empty($serverId) && $where .= " && (server_id='{$serverId}' OR server_name like '%{$serverId}%')";
            if(!empty($payType)){
                if($payType == 'wxpay'){
                    $where .= " && left(pay_type, 5)='{$payType}'";
                }else{
                    $where .= " && left(pay_type, 6)='{$payType}'";
                }
            }

            if(!empty($gameResult)){
                if($gameResult == 1){
                    $where .= " && game_result='{$gameResult}'";
                }else{
                    $where .= " && game_result='0'";
                }
            }
            if(!empty($debug)){
                if($debug == 1){
                    $where .= " && debug='{$debug}'";
                }else{
                    $where .= " && debug='0'";
                }
            }
            if( !empty($channelId) ){
                if($channelId >= 5000){ //联运
                    $where .= " && channel_id='{$channelId}'";
                }else{ //CPS
                    $allChannelId = model('Channel')->allChannelId($channelId);
                    $where .= " && channel_id=1000 && reg_cps_id in ({$allChannelId})";
                }
            }

            if(input('?export')){
                $start = 0;
                $length = 0;
            }else{
                $start = ($page-1)*$limit;
                $length = $limit;
            }

            $y1 = date('Y', $date['start_time']);
            $y2 = date('Y', $date['end_time']);

            if($y1 == $y2){ //同一年
                $count = Db::name('PayOrderY_'.$y1)->where($where)->count();

                $select = Db::name('PayOrderY_'.$y1)
                    ->where($where)
                    ->limit($start, $length)
                    ->order('pay_time desc')
                    ->select();

                $sum = Db::name('PayOrderY_'.$y1)
                    ->where($where)
                    ->sum('total_price');
            }else{ //跨年
                $field = "server_id, server_name, role_id, role_name, my_order_id, game_id, channel_id, reg_cps_id, total_price, my_user_id, my_user_name, cp_order_id, ch_order_id, product_name, pay_type, pay_order_id, pay_time";
                $subQuery = Db::name('PayOrderY_'.$y1)
                    ->field($field)
                    ->where($where)
                    ->union("select {$field} from sdk_pay_order_y_{$y2} where ({$where})", true)
                    ->buildSql();

                $count = Db::table($subQuery.' y')->count();

                $select = Db::table($subQuery.' y')
                    ->limit($start, $length)
                    ->order('pay_time desc')
                    ->select();

                $sum = Db::table($subQuery.' y')
                    ->sum('total_price');
            }

            $gameIdName = model('Game')->gameIdName();
            $channelIdName = model('Channel')->channelIdName();

            $data = [];
            $data2 = [];
            foreach ($select as $k=>$v){
                if(substr($v['pay_type'], 0, 5)=='wxpay'){
                    $payType = '微信';
                }else if(substr($v['pay_type'], 0, 6)=='alipay'){
                    $payType = '支付宝';
                }else{
                    $payType = '';
                }

                $gameName = isset($gameIdName[$v['game_id']]) ? $gameIdName[$v['game_id']] : '';

                if($v['channel_id']==1000){
                    $channelId = $v['reg_cps_id'];
                    $channelName = isset($channelIdName[$v['reg_cps_id']]) ? $channelIdName[$v['reg_cps_id']] : '';
                }else{
                    $channelId = $v['channel_id'];
                    $channelName = isset($channelIdName[$v['channel_id']]) ? $channelIdName[$v['channel_id']]  : '';
                }

                $data[] = [
                    'my_order_id' => $v['my_order_id'],
                    'pay_time' => date('Y-m-d H:i:s', $v['pay_time']),
                    'type' => $v['channel_id']==1000 ? 'CPS' : '联运',
                    'game_id' => $v['game_id'] . '：' . $gameName,
                    'server_id' => $v['server_id'] . '：' . $v['server_name'],
                    'reg_cps_id' => $channelId . '：' .$channelName,
                    'total_price' => $v['total_price']/100,
                    'cp_order_id' => "\t" . $v['cp_order_id'] . "\t",
                    'ch_order_id' => "\t" . $v['ch_order_id'] . "\t",
                    'product_name' => $v['product_name'],
                    'pay_type' => $payType,
                    'pay_order_id' => "\t" . $v['pay_order_id'] . "\t",
                ];
                $data2[] = [
                    'my_order_id' => $v['my_order_id'],
                    'pay_time' => date('Y-m-d H:i:s', $v['pay_time']),
                    'channel_id' => $channelId . '：' . $channelName,
                    'game_id' => $v['game_id'] . '：' . $gameName,
                    'server_id' => $v['server_id'],
                    'server_name' => $v['server_name'],
                    'role_id' => $v['role_id'],
                    'role_name' => $v['role_name'],
                    'total_price' => $v['total_price']/100,
                    'cp_order_id' => "\t" . $v['cp_order_id'] . "\t",
                    'ch_order_id' => "\t" . $v['ch_order_id'] . "\t",
                    'pay_order_id' => "\t" . $v['pay_order_id'] . "\t",
                    'product_name' => $v['product_name'],
                ];
            }

            if(input('?export')){
                array_unshift($data2, ['米娱订单号','支付时间','渠道名称','游戏名称','区服ID','区服名称','角色ID','角色名称','充值金额','CP订单号','渠道订单号','支付单号','产品名称']);
                exportExcel($data2, input('date').'支付订单', input('date').'支付订单');
            }

            $this->layTable(0, '支付订单', $data, $count, round($sum/100, 2));
        }else{
            $date = date('Y-m-d', time()).' - '.date('Y-m-d', time());
            return view('', ['date'=>$date, 'date2'=>'2018-01 - 2018-01']);
        }
    }
}